package view;

import com.alibaba.druid.support.json.JSONUtils;
import entity.BookType;
import service.BookTypeServiceDaoImpl;
import function.BookTypeBaseDao2Impl;
import function.Druid;
import tool.MyTool;
import java.util.List;
import java.util.Scanner;


public class BookTypeView {
    Scanner sc = new Scanner(System.in);
    BookTypeBaseDao2Impl bookTypeBaseDao = BookTypeBaseDao2Impl.getBookTypeBaseDao();
    BookTypeServiceDaoImpl bookTypeService = new BookTypeServiceDaoImpl();
    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_CYAN = "\u001B[36m";
    /**
     * 对查询到的结果集进行操作
     *
     * @throws Exception
     */
    public void selectAndModify() throws Exception {

        while (true) {
            Integer id = null;
            System.out.print(ANSI_RESET + ANSI_GREEN);
            System.out.println("🔣-------~图书类型查询~-------🔣");
            System.out.println("🐕-------<1.通过id查询>------*");
            System.out.println("🐖-------<2.通过name查询>----*");
            System.out.println("🐖-------<3.退出>------------*");

            char c = MyTool.selectMenu(3).charAt(0);
            switch (c) {
                case '1':
                    id = bookTypeService.getBookTypeById();
                    if (id == null) {
                        System.out.println("退出成功");
                        return;
                    }
                    modify(id, null);
                    break;
                case '2':
                    id = bookTypeService.getBookTypeByName();
                    if (id == null) {
                        System.out.println("退出成功");
                        return;
                    }
                    modify(id, null);
                    break;
                case '3':
                    return;
            }
        }
    }

    /**
     * 图书类型主界面
     * @throws Exception
     */
    public void bookTypeMenu () throws Exception {
            while (true) {
                System.out.print(ANSI_RESET + ANSI_GREEN);
                System.out.println("🔣-------~书本类型~-------🔣");
                System.out.println("🐕-------<1.添加书本类型>------*");
                System.out.println("🐖-------<2.查询书本类型>----*");
                System.out.println("🐖-------<3.查询所有书本类型>------------*");
                System.out.println("🐱-------<4.删除读者类型>-----------*");
                System.out.println("🐱-------<5.退出>-----------*");

                char c = MyTool.selectMenu(5).charAt(0);
                switch (c) {
                    case '1':
                        bookTypeService.insert();
                        break;
                    case '2':
                        selectAndModify();
                        break;
                    case '3':
                        List<BookType> all = bookTypeService.getAll();
                        modify(null,all);
                        break;
                    case '4':
                        bookTypeService.deleteById();
                        break;
                    case '5':
                        return;
                }
            }
        }

        /**
         * 图书类型修改界面
         * @param id
         * @param arr
         * @throws Exception
         */
        public void modify(Integer id, List < BookType > arr) throws Exception {
            System.out.print(ANSI_RESET + ANSI_GREEN);
            System.out.println("🔣-------~修改~-------🔣");
            System.out.println("🐕-------<1.修改>------*");
            System.out.println("🐖-------<2.退出>----*");
            char c = MyTool.selectMenu(2).charAt(0);
            if (c == '2') {
                return;
            }
            Integer sid = id;
            if (id==null) {
                List<BookType> all = bookTypeBaseDao.getAll(Druid.getConnection());
                System.out.println("请选择你要修改书本类型的序号(直接按确定退出)");
                Integer index = MyTool.getTypeIndex(all.size());
                if(index==null){
                    System.out.println("退出成功");
                    return;
                }
                sid = all.get(index).getId();
            }
            if(sid!=null) {
                bookTypeService.update(sid);
            }
        }

    public static void main(String[] args) throws Exception {
        BookTypeView bookTypeView=new BookTypeView();
        bookTypeView.bookTypeMenu();
    }
}